package com.mind.api;

import com.alibaba.fastjson2.JSON;
import com.mind.api.vo.*;
import com.mind.common.utils.StringUtils;
import com.mind.system.domain.SysConfig;
import com.mind.system.mapper.SysConfigMapper;
import jakarta.annotation.Resource;
import lombok.extern.log4j.Log4j2;
import okhttp3.*;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@Component
@Log4j2
public class ShareAPI {

    @Resource
    private SysConfigMapper sysConfigMapper;

    private String baseUrl = "http://testitax.tongwei.com:8191";

    /**
     * 获取订单
     * @param orgCode 组织ID
     * @param orderNo 订单号
     * @return
     * @throws IOException
     */
    public OrderInfoResponse.OrderResult getOrderInfo(String orgCode, String orderNo) throws Exception {
        baseUrl = getBaseUrl();
        OkHttpClient client = new OkHttpClient();
        String params = JSON.toJSONString(new HashMap<String, Object>(){{
            put("Request",new EBSRequest(new OrderInfoRequest(orgCode, orderNo)));
                                          }});

        RequestBody body = RequestBody.create(params, MediaType.get("application/json; charset=utf-8"));

        // 创建请求

        Request request = new Request.Builder()
                .url(baseUrl + "/WP_ESB_SOA/APP_ITAX_SERVICE/Proxy_Services/TA_COM/queryOrderInfoRest_PS?_wadl")
                .post(body)
                .build();
        OrderInfoResponse orderInfoResponse;
        Response response = client.newCall(request).execute();
        String bodyStr = response.body().string();
        log.info("请求地址：{}, 请求参数： {}, 请求响应:{}",request.url(),params, bodyStr);
        // 获取响应
        if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

        // 打印响应体
        EBSResponse ebsResponse = JSON.parseObject(bodyStr, EBSResponse.class);
        orderInfoResponse = JSON.parseObject(ebsResponse.getResponse().getData(), OrderInfoResponse.class);
        return orderInfoResponse.getData();
    }

    /**
     * 获取发票详情
     * @param orgCode 组织ID
     * @param orderNo 订单号
     * @param invoiceTypes 订单号
     * @return
     * @throws IOException
     */
    public OrderDetailResponse.OrderDetailResult getOrderDetail(String orgCode, String orderNo, List<String> invoiceTypes) throws Exception {
        baseUrl = getBaseUrl();
        OkHttpClient client = new OkHttpClient();


        RequestBody body = RequestBody.create(JSON.toJSONString(new HashMap<String, Object>(){
                                                                    {
                                                                        put("Request", new EBSRequest(new OrderDetailRequest(orgCode, orderNo, invoiceTypes)));
                                                                    }}), MediaType.get("application/json; charset=utf-8"));

        // 创建请求
        Request request = new Request.Builder()
                .url(baseUrl + "/WP_ESB_SOA/APP_ITAX_SERVICE/Proxy_Services/TA_COM/getReimbursementedInvoiceInfoRest_PS?_wadl")
                .post(body)
                .build();
        OrderDetailResponse orderInfoResponse;
        Response response = client.newCall(request).execute();
        String bodyStr = response.body().string();
        log.info("请求地址：{}, 请求参数： {}, 请求响应:{}",request.url(),JSON.toJSONString(new EBSRequest(new OrderDetailRequest(orgCode, orderNo, invoiceTypes))), bodyStr);

        // 获取响应
        if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

        // 打印响应体
        EBSResponse ebsResponse = JSON.parseObject(bodyStr, EBSResponse.class);
        orderInfoResponse = JSON.parseObject(ebsResponse.getResponse().getData(), OrderDetailResponse.class);
        return orderInfoResponse.getData();
    }

    /**
     * 获取影像中的基础地址
     * @return
     */
    private String getBaseUrl(){
        SysConfig config = sysConfigMapper.checkConfigKeyUnique("SHARE_BASE_URL");
        if(StringUtils.isNull(config)){
            return baseUrl;
        }
        if(StringUtils.isEmpty(config.getConfigValue())){
            return baseUrl;
        }
        return config.getConfigValue();
    }

}
